/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Forms;

import BLL.BLLNguoiDung;
import DTO.DTONguoiDung;
import javax.swing.JOptionPane;

/**
 *
 * @author Documents
 */
public class DangNhap extends javax.swing.JDialog {

    BLLNguoiDung bllNgDung;//khai báo thuộc tính BllNguoidung
    public static String usr = "";

    /**
     * Creates new form DangNhap
     * @param parent
     * @param modal
     */
    public DangNhap(java.awt.Frame parent, boolean modal) {//hàm khởi tạo của Form Dang Nhap, nó sẽ chạy hàm hày đầu tiên khi form đc gọi
        super(parent, modal);
        initComponents();//khỏi tạo các phần tử trong form như các nút, menu....
        
        //khởi tạo BllNguoidung
        bllNgDung = new BLLNguoiDung();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTenDangNhap = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButtonDangNhap = new javax.swing.JButton();
        jButtonHuyBo = new javax.swing.JButton();
        txtMatKhau = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 153, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Đăng nhập");

        jLabel2.setText("Tên đăng nhập");

        txtTenDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTenDangNhap(evt);
            }
        });

        jLabel3.setText("Mật khẩu");

        jButtonDangNhap.setText("Đăng nhập");
        jButtonDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDangNhap(evt);
            }
        });

        jButtonHuyBo.setText("Hủy bỏ");
        jButtonHuyBo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonHuyBo(evt);
            }
        });

        txtMatKhau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(97, 97, 97)
                        .addComponent(jButtonDangNhap)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonHuyBo))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtTenDangNhap)
                                .addComponent(txtMatKhau, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)))))
                .addContainerGap(67, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonDangNhap)
                    .addComponent(jButtonHuyBo))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtTenDangNhap(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTenDangNhap
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTenDangNhap

    private void jButtonDangNhap(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDangNhap
        try {
            if ("".equals(checkInput())) {
                String tenDN = txtTenDangNhap.getText().trim();
                String mk = txtMatKhau.getText().trim();
                DTONguoiDung dtoNgDung = bllNgDung.ktraDangNhap(tenDN, mk);
                if (dtoNgDung != null) {
                    usr = dtoNgDung.getTenDangNhap();
                    JOptionPane.showMessageDialog(null, "Đăng nhập thành công!");
                    MainMenu.jButtonDNhap.setEnabled(false);
                    MainMenu.jButtonDoiMK.setEnabled(true);
                    MainMenu.jButtonDXuat.setEnabled(true);
                    MainMenu.jButtonBNhan.setEnabled(true);
                    MainMenu.jButtonNVien.setEnabled(true);
                    MainMenu.jButtonThKe.setEnabled(true);
                    MainMenu.jButtonNgDung.setEnabled(true);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Tên hoặc mật khẩu không đúng");
                }
            } else {
                JOptionPane.showMessageDialog(null, checkInput());
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Loi dang nhap");
        }
    }//GEN-LAST:event_jButtonDangNhap

    private void jButtonHuyBo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHuyBo
        this.dispose();
    }//GEN-LAST:event_jButtonHuyBo

    private void jPasswordField(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField
        // TODO add your handling code here:
    }//GEN-LAST:event_jPasswordField

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DangNhap dialog = new DangNhap(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonDangNhap;
    private javax.swing.JButton jButtonHuyBo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField txtMatKhau;
    private javax.swing.JTextField txtTenDangNhap;
    // End of variables declaration//GEN-END:variables

    private String checkInput() {
        String result="";
        if("".equals(txtTenDangNhap.getText().trim()))
            result+="vui lòng nhập tên người dùng\n";
        if("".equals(txtMatKhau.getText().trim()))
            result+="Vui lòng nhập mật khẩu";
        return result;
    }
}
